//
//  WmcNavVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/1.
//

import UIKit
import YPBedrock

// MARK: - [WmcNavVC]

class WmcNavVC: UINavigationController {
    // Lifecycle

    override init(rootViewController: UIViewController) {
        super.init(rootViewController: rootViewController)
        config()
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        config()
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    deinit {
        WmcLogManager.DebugLog(type: .delloc, log: "NavVC被释放\(self)")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // 转场动画
        delegate = self
    }
    
    override func popViewController(animated: Bool) -> UIViewController? {
        super.popViewController(animated: animated)
    }
    
    override func popToRootViewController(animated: Bool) -> [UIViewController]? {
        super.popToRootViewController(animated: animated)
    }

    // Internal

    /// 转场管理器
    var navTransition: WmcTransition?
    /// 根视图执行pop回调
    var rootPopVCBlock: YPBlock.Empty?
    /// push,pop完成之后的回调
    var navigationCompletionBlock: YPBlock.Empty?

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }

    func config() {
        // 水印相机全局使用自定义导航，隐藏系统导航
        navigationBar.isHidden = true
    }

    /// 推出
    func wmc_push(vc: UIViewController,
                  style: WmcTransition.Style?,
                  animated: Bool = true,
                  completion: YPBlock.Empty? = nil)
    {
        navigationCompletionBlock = completion
        if let style = style {
            navTransition = .init(operation: .push, style: style)
        } else {
            navTransition = nil
        }
        YPGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.pushViewController(vc, animated: animated)
        }
    }

    /// 返回
    func wmc_pop(style: WmcTransition.Style?,
                 animated: Bool = true,
                 completion: YPBlock.Empty? = nil)
    {
        navigationCompletionBlock = completion
        if let style = style {
            navTransition = .init(operation: .pop, style: style)
        } else {
            navTransition = nil
        }
        YPGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            if weakSelf.viewControllers.count <= 1 { // 跟视图
                weakSelf.rootPopVCBlock?()
            } else {
                weakSelf.popViewController(animated: animated)
            }
        }
    }

    func cacheManagement() {
        // 遍历vc，如果特定vc消失后，需要操作缓存清空
        var isHasVC: Bool = false
        viewControllers.forEach { vc in
            if let _ = vc as? WmcMediaBrowseVC {
                isHasVC = true
            }
        }
        if !isHasVC { // 没有了
            WmcImageBrowsePreview.ClearImageCacheMap()
        }
    }
}

// MARK: - UINavigationControllerDelegate

extension WmcNavVC: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationController.Operation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        navigationBar.isHidden = true
        cacheManagement()
        return navTransition
    }

    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        navigationCompletionBlock?()
    }
}
